package com.kb.order.service.impl;

import com.kb.order.dao.*;
import com.kb.order.pojo.*;
import com.kb.order.service.IShopInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ShopInfoService implements IShopInfoService {
    /**
     * 购物车
     */
    Map<Integer,ShopInfo> infoMap = new HashMap<Integer,ShopInfo>();

    @Resource
    private ShopInfoDao shopInfoDao;
    @Resource
    private FoodDao foodDao;
    @Resource
    private OrderDao orderDao;
    @Resource
    private OrderFoodDao orderFoodDao;
    @Resource
    private OrderDetailDao orderDetailDao;

    public int insert(ShopInfo pojo){
        return shopInfoDao.insert(pojo);
    }

    public int insertList(List< ShopInfo> pojos){
        return shopInfoDao.insertList(pojos);
    }

    public List<ShopInfo> select(ShopInfo pojo){
        return shopInfoDao.select(pojo);
    }

    public int update(ShopInfo pojo){
        return shopInfoDao.update(pojo);
    }

    /**
     * 查询购物车
     * @return
     */
    public Map<Integer,ShopInfo> lookCar(){
        return infoMap;
    }


    /**
     * 实现加入购物车功能
     * @param id
     */
    @Override
    public void joinCar(Integer id) {
        //Food food = foodDao.selectOne(id);
        //根据id查询到当前添加的商品和所在的店铺
        ShopInfo shopInfo = shopInfoDao.selectOne(id);
        System.out.println("数据库查询得到数据："+shopInfo);
        //循环遍历购物车集合
        if(infoMap.size()==0){
            System.out.println("添加食品了");
            //将当前店铺和食品添加到购物车集合中
            shopInfo.getFoodList().get(0).setFoodNum(1);
            infoMap.put(shopInfo.getId().intValue(),shopInfo);
        }else{
            for (Integer integer : infoMap.keySet()) {
                //如果相等则证明当前购物车内已有当前添加食品的店铺
                if(shopInfo.getId().intValue()==integer){
                    //将食品加入到当前店铺中
                    infoMap.get(integer).getFoodList().add(shopInfo.getFoodList().get(0));
                    infoMap.get(integer).getFoodList().get(0).setFoodNum(1);
                }else {
                    System.out.println("添加食品了");
                    //将当前店铺和食品添加到购物车集合中
                    shopInfo.getFoodList().get(0).setFoodNum(1);
                    infoMap.put(shopInfo.getId().intValue(),shopInfo);
                }
            }
        }
    }

    /**
     * 实现删除购物车删除商品功能
     * @param id
     */
    @Override
    public void delCarFood(Integer id) {
        //根据id查询到当前删除的商品和所在的店铺
        ShopInfo shopInfo = shopInfoDao.selectOne(id);
        //循环遍历购物车集合
        for (Integer integer : infoMap.keySet()) {
            //判断需要删除的商品所在的店铺
            if(infoMap.get(integer).getId()==shopInfo.getId()){
                //循环遍历当前店铺中所有商品
                List<Food> foodList = infoMap.get(integer).getFoodList();
                for (int i = foodList.size()-1; i >= 0; i--) {

                    Food food = foodList.get(i);
                    //判断是否为需要删除的商品
                    if(food.getId().intValue()==id){
                        foodList.remove(food);
                    }
                }
            }
        }
        //判断店铺中是否有商品
        delCarShop();
    }

    /**
     * 实现对购物车中的食品加一
     * @param id
     */
    @Override
    public void plusFoodNum(Integer id) {
        //根据id查询到当前删除的商品和所在的店铺
        ShopInfo shopInfo = shopInfoDao.selectOne(id);
        //循环遍历购物车集合
        for (Integer integer : infoMap.keySet()) {
            //判断需要删除的商品所在的店铺
            if(infoMap.get(integer).getId()==shopInfo.getId()){
                //循环遍历当前店铺中所有商品
                List<Food> foodList = infoMap.get(integer).getFoodList();
                for (Food food : foodList) {
                    //判断是否为需要加一的商品
                    if(food.getId().intValue()==id){
                        if(food.getLimitNum().intValue()==food.getFoodNum()){
                            //达到限购数，无法加一
                        }else {
                            food.setFoodNum(food.getFoodNum()+1);
                        }
                    }
                }
            }
        }
        //判断店铺中是否有商品
        delCarShop();
    }

    @Override
    public void loseFoodNum(Integer id) {
        //根据id查询到当前删除的商品和所在的店铺
        ShopInfo shopInfo = shopInfoDao.selectOne(id);
        //循环遍历购物车集合
        for (Integer integer : infoMap.keySet()) {
            //判断需要删除的商品所在的店铺
            if(infoMap.get(integer).getId()==shopInfo.getId()){
                //循环遍历当前店铺中所有商品
                List<Food> foodList = infoMap.get(integer).getFoodList();
                for (Food food : foodList) {
                    //判断是否为需要加一的商品
                    if(food.getId().intValue()==id){
                        if(food.getLimitNum().intValue()==1){
                            //达到0时，无法减一，直接删除
                            foodList.remove(food);
                        }else {
                            food.setFoodNum(food.getFoodNum()-1);
                        }
                    }
                }
            }
        }
        //判断店铺中是否有商品
        delCarShop();
    }



    @Override
    public void addOrder(Order order,Object[] foodId,Integer addressId) {
        //订单主表添加
        order.setUserId(new Long(1));
        //随机生成
        /*Random random = new Random();
        String num = "";
        for (int i = 0; i < 10; i++) {
            num += random.nextInt(9);
        }*/
        order.setOrderId(new Date().toString());
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        order.setAddTime(new Long(Long.parseLong(format.format(new Date()))));
        orderDao.insert(order);

        //添加订单地址
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderId(order.getOrderId());
        //添加用户信息
        //orderDetail.setUserUsername();
        //orderDetail.setUserMobile();
        orderDetail.setUserAddressId(addressId.longValue());
        orderDetailDao.insert(orderDetail);

        //订单商品详情表
        for (Integer integer : infoMap.keySet()) {
            List<Food> foodList = infoMap.get(integer).getFoodList();
            for (Food food : foodList) {
                for (Object o : foodId) {
                    System.out.println(o+"对比"+food.getId());
                    if(Long.parseLong(o.toString())==food.getId()){
                        OrderFood orderFood = new OrderFood();
                        orderFood.setOrderId(order.getOrderId());
                        orderFood.setShopId(infoMap.get(integer).getId());
                        orderFood.setShopname(infoMap.get(integer).getShopname());
                        orderFood.setFoodId(food.getId());
                        orderFood.setOriginPrice(food.getOriginPrice());
                        orderFood.setSellPrice(food.getSellPrice());
                        orderFood.setNumber(food.getFoodNum().longValue());
                        orderFoodDao.insert(orderFood);
                        //将已经加入订单的食品从购物车中删除
                        delCarFood(food.getId().intValue());
                        delCarShop();
                    }
                }
            }
        }

    }

    /**
     * 判断购物车中，当一个店铺下已经无食品时，删除当前店铺
     */
    public void delCarShop(){
        for (Integer integer : infoMap.keySet()) {
            if(infoMap.get(integer).getFoodList().size()==0){
                infoMap.remove(integer);
            }
        }
    }

    /**
     * 查询店铺
     * @param pojo
     * @return
     */
    public List<ShopInfo> selectshop(ShopInfo pojo){

        return shopInfoDao.selectshop(pojo);
    }
}
